<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
              @change="dateChange"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input v-model="userInfo.departmentName" class="inputW" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- 封装 -->
            <upload-img :imgUrl.sync="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailByIdAPI, saveUserDetailByIdAPI } from '@/api'
import EmployeeEnum from '@/api/constant/employees'
import { parseTime } from '@/utils'

export default {
  data () {
    return {
      userId: this.$route.query.id,
      EmployeeEnum, // 员工枚举数据
      userInfo: {}
    }
  },
  created () {
    // 调用获取用户信息的 API
    this.loadUserInfoById()
  },
  methods: {
    // 加载用户信息
    async loadUserInfoById () {
      const res = await getUserDetailByIdAPI(this.userId).catch(err => err)
      if (!res.success) return this.$message.error(res.message)
      this.userInfo = res.data
      // 时间问题思路
      // el-date-picker组件内腰的是日期对象
      // 后台要的是日期字符串（年-月-日）
      // 后端ajax接收的响应，就是后台录入了什么就返回什么
      // 解决办法new Date（'2022-06-6'）要求格式必须是年-月-日字符串才可以
      this.userInfo.timeOfEntry = new Date(res.data.timeOfEntry)//赋予日期
    },
    // 保存用户信息
    async saveUser () {
      if (this.userInfo.timeOfEntry instanceof Date) { //判断如果用户没有更新时间，timeOfEntry还是日期对象，进行判断
        // 把日期对象转成字符串，强制覆盖给v-model关联的变量
        this.userInfo.timeOfEntry = parseTime(this.userInfo.timeOfEntry).slice('')[0]
      }
      const res = await saveUserDetailByIdAPI(this.userInfo)
      console.log(res)
    },
    // 日期选择，改变事件对象，因后台让返回的是字符串
    dateChange (data) {
      console.log(data)//Sun Nov 04 2018 00:00:00 GMT+0800 (中国标准时间)
      // 用已封装好的
      // 把日期对象转成字符串，强制覆盖给v-model关联的变量
      this.userInfo.timeOfEntry = parseTime(data).slice('')[0]

    }

  }
}
</script>

<style scoped lang="scss">
.user-info {
  padding-top: 20px;
}
</style>